package com.financing.wap.service.impl;


import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.financing.wap.common.CommonConstantUtil;
import com.financing.wap.dto.*;
import com.financing.wap.service.*;
import com.financing.wap.util.DateUtil;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import com.financing.wap.common.CommonConstantUtil.CommonStatus;
import com.financing.wap.dao.IDstIntegralRuleDao;
import com.financing.wap.util.DTOdateSetUtil;
import org.springframework.transaction.annotation.Transactional;

/**
 * dst_integral_rule  
 * 积分规则表 业务访问层实现类
 * @author huqiyun
 *
 */
 
@Service("dstIntegralRuleService")
public class DstIntegralRuleServiceImpl implements IDstIntegralRuleService{

    private Logger logger = Logger.getLogger(getClass());

	@Autowired
	private IDstIntegralRuleDao dstIntegralRuleDao;

	@Autowired
	private IDstUserIntegralFlowService dstUserIntegralFlowService;

    @Autowired
    private ICommonDstService commonDstService;

    @Autowired
    private IDstMemberInfoService dstMemberInfoService;

	@Autowired
	private IDstMemberSendLogService dstMemberSendLogService;

    @Autowired
    private IDstUserInfoExpandService dstUserInfoExpandService;

	@Autowired
	private IDstVirtualGoodsService dstVirtualGoodsService;

	@Autowired
	private IDstUserInfoService dstUserInfoService;


	@Override
	public int insert(DstIntegralRuleDTO dstIntegralRule) throws SQLException {
		DTOdateSetUtil.setCreateInfo(dstIntegralRule);
		DTOdateSetUtil.setUpdateInfo(dstIntegralRule);
		dstIntegralRule.setDeleteTag(CommonStatus.DELETE_TAG_NORMAL);
		return dstIntegralRuleDao.insert(dstIntegralRule);
	}

	@Override
	public void update(DstIntegralRuleDTO dstIntegralRule) throws SQLException {
		DTOdateSetUtil.setUpdateInfo(dstIntegralRule);
		dstIntegralRuleDao.update(dstIntegralRule);
	}

	@Override
	public void updateForce(DstIntegralRuleDTO dstIntegralRule) throws SQLException {
		DTOdateSetUtil.setUpdateInfo(dstIntegralRule);
		dstIntegralRuleDao.updateForce(dstIntegralRule);
	}

	@Override
	public void delete(Integer id) throws SQLException {
		dstIntegralRuleDao.delete(id);
	}
	
	@Override
	public List<DstIntegralRuleDTO> queryList(DstIntegralRuleDTO dstIntegralRule) throws SQLException {
		return dstIntegralRuleDao.queryList(dstIntegralRule);
	}
	@Override
	public DstIntegralRuleDTO loadByBusinessType(String businessType) throws SQLException {
		DstIntegralRuleDTO dstIntegralRule = new DstIntegralRuleDTO();
		dstIntegralRule.setRuleBusinessType(businessType);
		List<DstIntegralRuleDTO> list = dstIntegralRuleDao.queryList(dstIntegralRule);
		if(list==null||list.size()==0){
			return null;
		}
		return list.get(0);
	}

	@Override
	public int queryListCount(DstIntegralRuleDTO dstIntegralRule) throws SQLException {
		return dstIntegralRuleDao.queryListCount(dstIntegralRule);
	}

	@Override
	public DstIntegralRuleDTO loadById(Integer id) throws SQLException {
		return dstIntegralRuleDao.loadById(id);
	}
 	@Override
	public DstIntegralRuleDTO queryListGetFirst(DstIntegralRuleDTO dstIntegralRule)
			throws SQLException {
		 List<DstIntegralRuleDTO> queryList = queryList(dstIntegralRule);
		 if(queryList==null||queryList.size()==0){
			 return new DstIntegralRuleDTO();
		 }
		 return queryList.get(0);
	}
	@Override
    @Transactional(rollbackFor=Exception.class)
	public void execute(String openId,String businessType)
			throws Exception {

		DstIntegralRuleDTO integralRuleDTO = loadByBusinessType(businessType);
		if(integralRuleDTO == null ){
			throw new Exception("未找到积分规则"+businessType);
		}

		//判断是否已经上限 - 是否满足条件
		if(executeUpperLimit(openId,integralRuleDTO)){
            return;
        }

        DstMemberInfoDTO dstMemberInfo = new DstMemberInfoDTO();
        dstMemberInfo.setUserOpenid(openId);
        dstMemberInfo = dstMemberInfoService.queryListGetFirst(dstMemberInfo);
        
        boolean memberFlag = false;

        if(dstMemberInfo.getMemberEndDate() != null && dstMemberInfo.getMemberEndDate().after(new Date())){
            memberFlag = true;
        }

        DstUserIntegralFlowDTO dstUserIntegralFlow =  new DstUserIntegralFlowDTO();
        dstUserIntegralFlow.setCreateDate(new Date());
        dstUserIntegralFlow.setUpdateDate(new Date());
        dstUserIntegralFlow.setCreateBy("admin");
        dstUserIntegralFlow.setUpdateBy("admin");
        dstUserIntegralFlow.setUserOpenid(openId);
        dstUserIntegralFlow.setType(integralRuleDTO.getRuleBusinessType());
        dstUserIntegralFlow.setIntegralStatus(integralRuleDTO.getRuleIntegralStatus());

        //RuleMemberGrade 是会员，就是会员积分
        dstUserIntegralFlow.setGrade(memberFlag?Integer.valueOf(integralRuleDTO.getRuleMemberGrade()):Integer.valueOf(integralRuleDTO.getRuleGrade()));
        dstUserIntegralFlow.setBudget("1");
        dstUserIntegralFlow.setIntegralDate(DateUtil.getDate("yyyy-MM-dd"));
        dstUserIntegralFlowService.insert(dstUserIntegralFlow);

        //待领取 不更新会员积分
        if(!"1".equals(integralRuleDTO.getRuleIntegralStatus())) {
            //更新累计积分
            dstMemberInfo.setMemberScore(dstMemberInfo.getMemberScore() == null?0:dstMemberInfo.getMemberScore()  + dstUserIntegralFlow.getGrade());
            dstMemberInfoService.update(dstMemberInfo);
        }

	}

	@Override
    @Transactional(rollbackFor=Exception.class)
	public void executePay(String openId, Integer payProductId,String orderId) throws Exception {

		Date now = new Date();
        logger.info("in executePay begin!");

        logger.info("in executePay invite begin!");
		//积分处理
			//1、获取虚拟产品类型
			DstVirtualGoodsDTO virtualGoodsDTO = dstVirtualGoodsService.loadById(payProductId);
			if(virtualGoodsDTO == null){
                logger.error("in executePay invite virtualGoodsDTO is null!");
				return;
			}

			//2、是否有邀请人 有：邀请好友并开通会员  +300分
			DstUserInfoDTO dstUserInfo = new DstUserInfoDTO();
			dstUserInfo.setUserWechatOpenid(openId);
			dstUserInfo = dstUserInfoService.queryListGetFirst(dstUserInfo);
			if(StringUtils.isNotBlank(dstUserInfo.getUserParentOpenId())){
                //判断是否已经 +300
                DstUserIntegralFlowDTO dstUserIntegralFlow = new DstUserIntegralFlowDTO();
                dstUserIntegralFlow.setUserOpenid(openId);
                dstUserIntegralFlow.setTypeLike("buy");
                int parentCount = dstUserIntegralFlowService.queryListCount(dstUserIntegralFlow);

                logger.info("in executePay invite openId:"+openId);
                logger.info("in executePay invite parentCount:"+parentCount);

                if(parentCount <= 0){
                    execute(dstUserInfo.getUserParentOpenId(),"inviteOpenMembers");
                    logger.info("in executePay  invite 是否有邀请人 有：邀请好友并开通会员 end!");
                }

			}

            //3、购买会员赠送积分
            execute(openId,virtualGoodsDTO.getGoodsType());
            logger.info("in executePay  invite 购买会员赠送积分 end!");

        logger.info("in executePay invite begin!");

        logger.info("in executePay member begin!");

	   //会员处理
		   Integer memberDay = Integer.valueOf(virtualGoodsDTO.getGoodsField()) ;//虚拟会员-商品字段（会员时长）

			//1、更新用户会员信息
			DstMemberInfoDTO d = new DstMemberInfoDTO();
			d.setDeleteTag(CommonStatus.DELETE_TAG_NORMAL);
			d.setUserOpenid(openId);
			DstMemberInfoDTO memberInfo = dstMemberInfoService.queryListGetFirst(d);
			memberInfo.setMemberCharge("1");//收费会员

            memberInfo.setMemberStartDate(memberInfo.getMemberStartDate()== null ?now:memberInfo.getMemberStartDate());
            Integer memberDuration = memberInfo.getMemberDuration()==null?0:memberInfo.getMemberDuration();
            memberInfo.setMemberDuration(memberDuration+memberDay);

            Date memberEndDate = memberInfo.getMemberEndDate() == null?now :memberInfo.getMemberEndDate();//延长会员时间
            memberEndDate = memberEndDate.before(now)?now:memberEndDate;
            memberInfo.setMemberEndDate(DateUtil.addDay(memberEndDate,memberDay));

            memberInfo.setMemberLevel(Double.valueOf(memberInfo.getMemberDuration()/90));//会员等级
			dstMemberInfoService.update(memberInfo);

            logger.info("in executePay memberupdate  更新用户会员信息 end!");

		   //2、添加用户会员记录表
			DstMemberSendLogDTO dstMemberSendLog = new DstMemberSendLogDTO();
			dstMemberSendLog.setUserOpenid(openId);
			dstMemberSendLog.setUserParentOpenid(dstUserInfo.getUserParentOpenId());
			dstMemberSendLog.setMemberBuyDate(memberDay);
			dstMemberSendLog.setMemberBuyMoney(virtualGoodsDTO.getGoodsAmount());
		    dstMemberSendLog.setMemberSendType(virtualGoodsDTO.getGoodsType());

		    dstMemberSendLog.setMemberStartDate(memberEndDate);
			dstMemberSendLog.setMemberEndDate(DateUtil.addDay(memberEndDate,memberDay));

			dstMemberSendLog.setMemberBizId(orderId);

            //判断邀请人是否是会员
            Boolean parentMemberFlag = false;
            DstMemberInfoDTO parentMember = null;

            if(StringUtils.isNotBlank(dstUserInfo.getUserParentOpenId())){

                //1、更新用户会员信息
                parentMember = new DstMemberInfoDTO();
                parentMember.setDeleteTag(CommonStatus.DELETE_TAG_NORMAL);
                parentMember.setUserOpenid(dstUserInfo.getUserParentOpenId());
                parentMember = dstMemberInfoService.queryListGetFirst(parentMember);

                //邀请人是会员
                if(parentMember.getMemberEndDate()!=null&&parentMember.getMemberEndDate().after(new Date())){
                    dstMemberSendLog.setMemberCommission(virtualGoodsDTO.getGoodsAmount().divide(new BigDecimal(2)));
                    parentMemberFlag = true;
                }
            }

			Integer memberSendLogId =  dstMemberSendLogService.insert(dstMemberSendLog);
            logger.info("in executePay MemberSendLog add  添加用户会员记录表 end!");
            logger.info("in executePay MemberSendLog add  添加用户会员记录表 memberSendLogId:"+memberSendLogId);


            //邀请人不是会员 没有佣金
            if(parentMemberFlag){
                logger.info("in executePay MemberSendLog 邀请人是会员!");
                //判断是否已经领取过-同一个只能领取一次
                DstMemberSendLogDTO lq = new DstMemberSendLogDTO();
                lq.setUserOpenid(openId);
                lq.setUserParentOpenid(dstUserInfo.getUserParentOpenId());
                Boolean flag = true;
                List<DstMemberSendLogDTO> list =dstMemberSendLogService.queryList(lq);

                for(DstMemberSendLogDTO bean:list){

                    if(bean.getId().intValue() == memberSendLogId.intValue()){
                        //过滤本次提交
                        continue;
                    }
                    if(bean.getMemberBuyMoney()!= null && bean.getMemberBuyMoney().compareTo(BigDecimal.ZERO)>=0){
                        logger.info("in executePay MemberSendLog for getMemberBuyMoney:"+(bean.getMemberBuyMoney()));
                        flag = false;
                    }
                }
                logger.info("in executePay MemberSendLog flag:"+flag);
                if(flag) {

                    BigDecimal memberAllMoney = parentMember.getMemberAllMoney() == null ?BigDecimal.ZERO: parentMember.getMemberAllMoney();
                    BigDecimal memberMoney = parentMember.getMemberMoney() == null ?BigDecimal.ZERO: parentMember.getMemberMoney();
                    BigDecimal memberCommission = dstMemberSendLog.getMemberCommission() == null ?BigDecimal.ZERO: dstMemberSendLog.getMemberCommission();
                    parentMember.setMemberAllMoney(memberAllMoney.add(memberCommission)); //用户总佣金
                    parentMember.setMemberMoney(memberMoney.add(memberCommission)); //用户当前佣金余额
                    dstMemberInfoService.update(parentMember);

                }
            }

            logger.info("in executePay member end!");

	}

	@Override
    @Transactional(rollbackFor=Exception.class)
	public void executeSubscribe(String openId) throws Exception {

		Date now = new Date();

		//关注
		//1、用户更新会员信息，邀请人判断是否超过20人关注

		DstUserInfoDTO dstUserInfo = new DstUserInfoDTO();
		dstUserInfo.setUserWechatOpenid(openId);
		dstUserInfo = dstUserInfoService.queryListGetFirst(dstUserInfo);

        logger.info(" executeSubscribe begin:"+openId);

        Map<String,Boolean> map = new HashMap<String, Boolean>();
        map.put("memberCharge",false);
		if(StringUtils.isNotBlank(dstUserInfo.getUserParentOpenId())){
           String  parentOpenId = dstUserInfo.getUserParentOpenId();
            //邀请人处理逻辑 判断是否邀请超过20的倍数的人数关注   %20  判断赠送1个月
           judgeCount(openId, now, map, parentOpenId);

		}

        //1、用户-判断是否已经领取免费体验
        DstMemberSendLogDTO query = new DstMemberSendLogDTO();
        query.setUserOpenid(openId);
        query.setMemberSendType("freeSevenDay");
        int count = dstMemberSendLogService.queryListCount(query);

        logger.info(" executeSubscribe freeSevenDayCount:"+count);
        if(count > 0){
            return;
        }

        //获取 体验7天麒麟会员
        DstVirtualGoodsDTO dstVirtualGoods = new DstVirtualGoodsDTO();
        dstVirtualGoods.setGoodsType("freeSevenDay");// 体验7天麒麟会员
        dstVirtualGoods = dstVirtualGoodsService.queryListGetFirst(dstVirtualGoods);
        if(dstVirtualGoods == null){
            logger.error(" executeSubscribe DstVirtualGoodsDTO freeSevenDay is null ");
            return;
        }

        //2、用户-更新关注用户会员信息表

		DstMemberInfoDTO member = new DstMemberInfoDTO();
		member.setDeleteTag(CommonStatus.DELETE_TAG_NORMAL);
		member.setUserOpenid(openId);
		member = dstMemberInfoService.queryListGetFirst(member);
		member.setMemberStartDate(member.getMemberStartDate()== null ?now:member.getMemberStartDate());

        logger.info(" executeSubscribe memberCharge:"+map.get("memberCharge"));
        
        
    	//3、体验7天麒麟会员 记录
        DstMemberSendLogDTO dstMemberSendLog = new DstMemberSendLogDTO();
        dstMemberSendLog.setUserOpenid(openId);
        dstMemberSendLog.setUserParentOpenid(dstUserInfo.getUserParentOpenId());
        dstMemberSendLog.setMemberBuyDate(Integer.valueOf(dstVirtualGoods.getGoodsField()));
        dstMemberSendLog.setMemberSendType(dstVirtualGoods.getGoodsType());
        dstMemberSendLog.setMemberStartDate(now);
        dstMemberSendLog.setMemberEndDate(DateUtil.addDay(now,Integer.valueOf(dstVirtualGoods.getGoodsField())));
        dstMemberSendLogService.insert(dstMemberSendLog);

        if(map.get("memberCharge")){

            DstVirtualGoodsDTO inviteVirtualGoods = new DstVirtualGoodsDTO();
            inviteVirtualGoods.setGoodsType("freeInviteSevenDay");// 免费赠送好友7天麒麟会员
            inviteVirtualGoods = dstVirtualGoodsService.queryListGetFirst(inviteVirtualGoods);
            if(inviteVirtualGoods == null){
                logger.error(" executeSubscribe DstVirtualGoodsDTO freeSevenDay is null ");
                return;
            }
            member.setMemberDuration(member.getMemberDuration()+Integer.valueOf(dstVirtualGoods.getGoodsField())+Integer.valueOf(inviteVirtualGoods.getGoodsField()));
            Date memberEndDate = member.getMemberEndDate() == null?now :member.getMemberEndDate();//延长会员时间
            memberEndDate = memberEndDate.before(now)?now:memberEndDate;
            member.setMemberEndDate(DateUtil.addDay(memberEndDate,Integer.valueOf(dstVirtualGoods.getGoodsField())+Integer.valueOf(inviteVirtualGoods.getGoodsField())));
            //4、免费赠送好友7天麒麟会员
            DstMemberSendLogDTO dstMemberSendLog2 = new DstMemberSendLogDTO();
            dstMemberSendLog2.setUserOpenid(openId);
            dstMemberSendLog2.setUserParentOpenid(dstUserInfo.getUserParentOpenId());
            dstMemberSendLog2.setMemberBuyDate(Integer.valueOf(inviteVirtualGoods.getGoodsField()));
            dstMemberSendLog2.setMemberSendType(inviteVirtualGoods.getGoodsType());
            dstMemberSendLog2.setMemberStartDate(dstMemberSendLog.getMemberEndDate());
            dstMemberSendLog2.setMemberEndDate(DateUtil.addDay(dstMemberSendLog.getMemberEndDate(),Integer.valueOf(inviteVirtualGoods.getGoodsField())));
            dstMemberSendLogService.insert(dstMemberSendLog2);
            logger.info(" executeSubscribe freeInviteSevenDay add end！");
        }else{
            int duration = member.getMemberDuration() ==  null ?0 :member.getMemberDuration();
            member.setMemberDuration(duration+Integer.valueOf(dstVirtualGoods.getGoodsField()));
            Date memberEndDate = member.getMemberEndDate() == null?now :member.getMemberEndDate();
            memberEndDate = memberEndDate.before(now)?now:memberEndDate;
            member.setMemberEndDate(DateUtil.addDay(memberEndDate,Integer.valueOf(dstVirtualGoods.getGoodsField())));
        }
        logger.info(" executeSubscribe executeSubscribe  updateMember openid:"+member.getUserOpenid());
        member.setMemberLevel(Double.valueOf(member.getMemberDuration()/90));//会员等级
		dstMemberInfoService.update(member);
        logger.info(" executeSubscribe freeSevenDay add end！");

	}


    @Override
    @Transactional(rollbackFor=Exception.class)
    public void executeAuth(String openId) throws Exception {

        Date now = new Date();

        //1、用户-判断是否已经领取免费体验
        DstMemberSendLogDTO query = new DstMemberSendLogDTO();
        query.setUserOpenid(openId);
        query.setMemberSendType("freeSevenDay");
        int count = dstMemberSendLogService.queryListCount(query);

        logger.info(" executeAuth freeSevenDayCount:"+count);
        if(count > 0){
            return;
        }

        //获取 体验7天麒麟会员
        DstVirtualGoodsDTO dstVirtualGoods = new DstVirtualGoodsDTO();
        dstVirtualGoods.setGoodsType("freeSevenDay");// 体验7天麒麟会员
        dstVirtualGoods = dstVirtualGoodsService.queryListGetFirst(dstVirtualGoods);
        if(dstVirtualGoods == null){
            logger.error(" executeAuth DstVirtualGoodsDTO freeSevenDay is null ");
            return;
        }

        //2、用户-更新关注用户会员信息表

        DstMemberInfoDTO member = new DstMemberInfoDTO();
        member.setDeleteTag(CommonStatus.DELETE_TAG_NORMAL);
        member.setUserOpenid(openId);
        member = dstMemberInfoService.queryListGetFirst(member);
        member.setMemberStartDate(member.getMemberStartDate()== null ?now:member.getMemberStartDate());

        int duration = member.getMemberDuration() ==  null ?0 :member.getMemberDuration();
        member.setMemberDuration(duration+Integer.valueOf(dstVirtualGoods.getGoodsField()));

        Date memberEndDate = member.getMemberEndDate() == null?now :member.getMemberEndDate();
        memberEndDate = memberEndDate.before(now)?now:memberEndDate;
        member.setMemberEndDate(DateUtil.addDay(memberEndDate,Integer.valueOf(dstVirtualGoods.getGoodsField())));

        logger.info(" executeAuth executeSubscribe  updateMember openid:"+member.getUserOpenid());
        member.setMemberLevel(Double.valueOf(member.getMemberDuration()/90));//会员等级

        dstMemberInfoService.update(member);

        //4、体验7天麒麟会员 记录

        DstMemberSendLogDTO dstMemberSendLog = new DstMemberSendLogDTO();
        dstMemberSendLog.setUserOpenid(openId);
        dstMemberSendLog.setMemberBuyDate(Integer.valueOf(dstVirtualGoods.getGoodsField()));
        dstMemberSendLog.setMemberSendType(dstVirtualGoods.getGoodsType());
        dstMemberSendLog.setMemberStartDate(memberEndDate);
        dstMemberSendLog.setMemberEndDate(DateUtil.addDay(memberEndDate,Integer.valueOf(dstVirtualGoods.getGoodsField())));
        dstMemberSendLogService.insert(dstMemberSendLog);

        logger.info(" executeAuth freeSevenDay add end！");

    }

    private void judgeCount(String openId, Date now, Map<String,Boolean> map,String parentOpenId) throws SQLException {

        logger.info(" executeSubscribe judgeCount openId:"+openId);
        logger.info(" executeSubscribe judgeCount  parentOpenId:"+parentOpenId);

        DstMemberInfoDTO parentMember = new DstMemberInfoDTO();
        parentMember.setDeleteTag(CommonStatus.DELETE_TAG_NORMAL);
        parentMember.setUserOpenid(parentOpenId);
        parentMember = dstMemberInfoService.queryListGetFirst(parentMember);

        if(parentMember.getId() == null){
            logger.error(" executeSubscribe parentMember is null parentOpenId:"+parentOpenId);
            return;
        }

        //邀请人是否是会员
        if(parentMember.getMemberEndDate() != null && parentMember.getMemberEndDate().after(new Date())){
            map.put("memberCharge",true);
        }

        logger.info(" executeSubscribe judgeCount parent memberCharge:"+map.get("memberCharge"));

        //2、 邀请人不为空，判断是否邀请超过20的倍数的人数关注   %20  判断赠送1个月

        //2-1、先获取邀请领取1个月会员虚拟商品
        DstVirtualGoodsDTO dstVirtualGoods = new DstVirtualGoodsDTO();
        dstVirtualGoods.setGoodsType("freeFollowMember");
        dstVirtualGoods = dstVirtualGoodsService.queryListGetFirst(dstVirtualGoods);
        if(dstVirtualGoods == null){
            logger.error(" executeSubscribe DstVirtualGoodsDTO freeFollowMember is null ");
            return;
        }
        int configCount = StringUtils.isBlank(dstVirtualGoods.getGoodsExplain())?20:Integer.valueOf(dstVirtualGoods.getGoodsExplain());

        logger.info(" executeSubscribe judgeCount DstVirtualGoodsDTO configCount:"+configCount);

        //2-2、获取用户邀请人数
        DstUserInfoDTO judge = new DstUserInfoDTO();
        judge.setUserParentOpenId(parentOpenId);
        judge.setFlagConcern("1");
        judge.setDeleteTag(CommonStatus.DELETE_TAG_NORMAL);
        int judgeCount = dstUserInfoService.queryListCount(judge);//获取邀请人数

        logger.info(" executeSubscribe judgeCount parent judgeCount:"+judgeCount);

        //2-3 满20人 处理  配置读取
        if((judgeCount/configCount)>0) {

            int js = (judgeCount/configCount);
            logger.info(" executeSubscribe judgeCount parent js:"+js);

            //获取父用户已经领取的数量
            DstMemberSendLogDTO judgeQuery = new DstMemberSendLogDTO();
            judgeQuery.setUserOpenid(parentOpenId);
            judgeQuery.setMemberSendType("freeFollowMember");
            int judgeQueryCount = dstMemberSendLogService.queryListCount(judgeQuery);

            logger.info(" executeSubscribe judgeCount parent judgeCountLQ:"+judgeQueryCount);

            if(js > judgeQueryCount){

                parentMember.setMemberStartDate(parentMember.getMemberStartDate()== null ?now:parentMember.getMemberStartDate());
                int duration = parentMember.getMemberDuration() ==  null ?0 :parentMember.getMemberDuration();
                parentMember.setMemberDuration(duration+Integer.valueOf(dstVirtualGoods.getGoodsField()));

                Date memberEndDate = parentMember.getMemberEndDate() == null?now :parentMember.getMemberEndDate();
                memberEndDate = memberEndDate.before(now)?now:memberEndDate;
                parentMember.setMemberEndDate(DateUtil.addDay(memberEndDate,Integer.valueOf(dstVirtualGoods.getGoodsField())));
                parentMember.setMemberLevel(Double.valueOf(parentMember.getMemberDuration()/90));//会员等级
                dstMemberInfoService.update(parentMember);

                //3、邀请用户会员记录表

                DstMemberSendLogDTO dstMemberSendLog = new DstMemberSendLogDTO();
                dstMemberSendLog.setUserOpenid(parentOpenId);
                dstMemberSendLog.setMemberBuyDate(Integer.valueOf(dstVirtualGoods.getGoodsField()));
                dstMemberSendLog.setMemberSendType(dstVirtualGoods.getGoodsType());
                dstMemberSendLog.setMemberStartDate(memberEndDate);
                dstMemberSendLog.setMemberEndDate(DateUtil.addDay(memberEndDate,Integer.valueOf(dstVirtualGoods.getGoodsField())));
                dstMemberSendLogService.insert(dstMemberSendLog);

                logger.info(" executeSubscribe freeFollowMember add end！");
            }
        }
    }


    //判断是否已经上限  true 上限，false  未
	public boolean executeUpperLimit(String openId,DstIntegralRuleDTO integralRuleDTO)
			throws SQLException {

		String businessType =  integralRuleDTO.getRuleBusinessType();

		if("userCard".equals(businessType)) {

            DstUserInfoExpandDTO addInfo=new DstUserInfoExpandDTO();
            addInfo.setUserOpenid(openId);
            DstUserInfoExpandDTO userInfoExpand = dstUserInfoExpandService.queryListGetFirst(addInfo);


            //姓名、电话、二维吗各占进度20%，公司、邮箱、职务、地址各占10%合计100%
            if(StringUtils.isBlank(userInfoExpand.getUserName()) ||
                    StringUtils.isBlank(userInfoExpand.getUserCardImg()) ||
                    StringUtils.isBlank(userInfoExpand.getUserPhone())||
                    StringUtils.isBlank(userInfoExpand.getUserCompany())||
                    StringUtils.isBlank(userInfoExpand.getUserEmail())||
                    StringUtils.isBlank(userInfoExpand.getUserPosition())||
					StringUtils.isBlank(userInfoExpand.getUserAddress())){
                return true;
            }

			//设置名片-一个用户一次
			DstUserIntegralFlowDTO query = new DstUserIntegralFlowDTO();
			query.setUserOpenid(openId);
			query.setType(businessType);
			int count = dstUserIntegralFlowService.queryListCount(query);
			return count > 0 ?true:false;

		}else if("shareArticle".equals(businessType)){

            logger.info("executeUpperLimit shareArticle:"+ openId);

            Map<String,String> map = new HashMap<String,String>();
            map.put("shareArticleDate",DateUtil.getDate("yyyy-MM-dd"));
            map.put("openid",openId);
            int count =  commonDstService.shareArticleExecuteUpperLimit(map);
            logger.info("shareArticleExecuteUpperLimit:"+ count);
            //分享文章
            if(count <= 0){
                return true;
            }

            //分享-一天10次
            DstUserIntegralFlowDTO query = new DstUserIntegralFlowDTO();
            query.setUserOpenid(openId);
            query.setType(businessType);
            query.setIntegralDate(DateUtil.getDate("yyyy-MM-dd"));
            int visitCount = dstUserIntegralFlowService.queryListCount(query);

            logger.info("shareArticle shareArticleCount:"+ visitCount);

            //当天访客数每递增10人 添加一天记录 <= visitCount，记录已经添加
            if(count <= visitCount){
                return true;
            }

            return visitCount >= integralRuleDTO.getRuleFrequencyType();

		}else if("visitArticlePeople".equals(businessType)){

            logger.info("executeUpperLimit visitArticlePeople:"+ openId);

			//查询拜访记录
            Map<String,String> map = new HashMap<String,String>();
            map.put("visitDate",DateUtil.getDate("yyyy-MM-dd"));
            map.put("openid",openId);
            int count =  commonDstService.visitExecuteUpperLimit(map);
            logger.info("visitArticlePeople:"+ count);
            //当天访客数每递增10人 添加积分  <10 不添加
            if((count/10) <= 0){

                return true;
            }

            //拜访积分-一天10次
            DstUserIntegralFlowDTO query = new DstUserIntegralFlowDTO();
            query.setUserOpenid(openId);
            query.setType(businessType);
            query.setIntegralDate(DateUtil.getDate("yyyy-MM-dd"));
            int visitCount = dstUserIntegralFlowService.queryListCount(query);

            logger.info("visitArticlePeople visitCount:"+ visitCount);

            //当天访客数每递增10人 添加一天记录 <= visitCount，记录已经添加
            if((count/10) <= visitCount){
                return true;
            }

            return visitCount >= integralRuleDTO.getRuleFrequencyType();
		}else if("inviteOpenMembers".equals(businessType)){

            logger.info("executeUpperLimit visitArticlePeople:"+ openId);

            //查询拜访记录
            Map<String,String> map = new HashMap<String,String>();
            map.put("visitDate",DateUtil.getDate("yyyy-MM-dd"));
            map.put("openid",openId);
            int count =  commonDstService.visitExecuteUpperLimit(map);
            logger.info("visitArticlePeople:"+ count);
            //当天访客数每递增10人 添加积分  <10 不添加
            if((count/10) <= 0){

                return true;
            }

            //拜访积分-一天10次
            DstUserIntegralFlowDTO query = new DstUserIntegralFlowDTO();
            query.setUserOpenid(openId);
            query.setType(businessType);
            query.setIntegralDate(DateUtil.getDate("yyyy-MM-dd"));
            int visitCount = dstUserIntegralFlowService.queryListCount(query);

            logger.info("visitArticlePeople visitCount:"+ visitCount);

            //当天访客数每递增10人 添加一天记录 <= visitCount，记录已经添加
            if((count/10) <= visitCount){
                return true;
            }

            return visitCount >= integralRuleDTO.getRuleFrequencyType();
        }

		return false;
	}
}
